package com.example.gallery;


import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.Toast;

import com.example.board.R;



public class JoinActivity extends Activity implements OnCheckedChangeListener, OnClickListener, OnTouchListener{
	private InputMethodManager ipm;
	final static String TAG = "JOIN ACTIVITY";
	EditText join_id;
	EditText join_pw;
	CheckBox join_chk_pw;
	ScrollView scrollView;

	Button btn_terms;
	Handler handler;
	
	int scrollX;
	int scrollY;
	int upX;
	int upY;
	int downX;
	int downY;
	int movingX;
	int movingY;
	int succ;
	String id;
	String pw;
	String imei;
	String cache_path;

	MemberDTO info;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.join);
		
		//Ű���� ���⸦ �� inputmanager ��ü
		ipm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		
		Log.i(TAG, TAG);
		join_id = (EditText) findViewById(R.id.join_id);
		join_pw = (EditText) findViewById(R.id.join_pw);
		join_chk_pw = (CheckBox) findViewById(R.id.join_chk_pw);
		scrollView = (ScrollView) findViewById(R.id.scrollView);

		btn_terms = (Button) findViewById(R.id.btn_terms);
		
		join_chk_pw.setOnCheckedChangeListener(this);
		scrollView.setOnTouchListener(this);
		btn_terms.setOnClickListener(this);
		
//		��й�ȣ �Է� �� Ű���� ������
		ipm.hideSoftInputFromWindow(join_pw.getWindowToken(), 0);
		
		handler = new Handler();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_terms:	//���� ���Թ�ư
			if("".equals(join_id.getText().toString())){
				Toast.makeText(getApplicationContext(), "���̵� �Է��� �ּ���", Toast.LENGTH_SHORT).show();
				return;
			}
			else if("".equals(join_pw.getText().toString())){
				Toast.makeText(getApplicationContext(), "��й�ȣ�� �Է��� �ּ���", Toast.LENGTH_SHORT).show();
				return;
			}
			else{
				MemberDTO dto = null;
				MemberHandler db = MemberHandler.open(this);
				id = join_id.getText().toString();
				pw = join_pw.getText().toString();
				MemberDTO member = new MemberDTO(id, pw);
				
				succ = db.checkID(member);
				Log.i(TAG, succ+"");
				if(succ > 0){
					long rs = 0;
					rs = db.join(member);
					if(rs > 0){
						Toast.makeText(getApplicationContext(), id+"�� ȯ���մϴ�\n�α��� �� ����� �ּ���", Toast.LENGTH_SHORT).show();
						goMain();
					}
					else{
						Toast.makeText(getApplicationContext(), id+"���� �� ����߻�\n�ٽ� �õ��� �ּ���", Toast.LENGTH_SHORT).show();
						return;
					}
				}
				else{
					Toast.makeText(getApplicationContext(), "�ߺ��Ǵ� ID�Դϴ�\n�ٽ��Է� �� �ּ���", Toast.LENGTH_SHORT).show();
				}
			}
			break;

		default:
			
			break;
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) {
		case R.id.join_chk_pw:
			if(isChecked){	//��й�ȣ üũ ǥ�� �� ��� text�� ����
				join_pw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
				Log.i(TAG, "checked");
				
			}
			else{	
				join_pw.setTransformationMethod(PasswordTransformationMethod.getInstance());
				Log.i(TAG, "unchecked");
			}
			break;

		default:
			break;
		}
	}
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_MOVE:	//��ġ�� �����̴� ���� event
			movingX = (int) event.getRawX();
			movingY = (int) event.getRawY();
//			Log.i(TAG, "ACTION_MOVE x : "+movingX+" y: "+movingY);
			break;
		case MotionEvent.ACTION_DOWN:	//ó�� ��ġ�� event
			downX = (int) event.getRawX();
			downY = (int) event.getRawY();
			Log.i(TAG, "ACTION_DOWN x : "+downX+" y: "+downY);
			break;
		case MotionEvent.ACTION_UP:		//��ġ���� ���� ������� event
			upX = (int) event.getRawX();
			upY = (int) event.getRawY();
			Log.i(TAG, "ACTION_UP x : "+upX+" y: "+upY);
			Log.i(TAG, String.valueOf(downY - upY ));
			break;

		default:
//			x = (int) event.getRawX();
//			y = (int) event.getRawY();
			Log.i(TAG, "Default ");
			break;
		}
		if(downY - upY >= 120 && event.getAction() == MotionEvent.ACTION_UP){
			handler.post(runner);
			Log.i(TAG, String.valueOf(downY - upY ));
		}
		return false;
	}
	
	public Runnable runner = new Runnable() {
		
		@TargetApi(Build.VERSION_CODES.JELLY_BEAN)	//target�� ������ ������ ��������� �� ���ư�����??
		@Override
		public void run() {
			btn_terms.setBackground(getResources().getDrawable(R.drawable.btn_active));
			btn_terms.setEnabled(true);
		}
	};
	
	public void goMain(){
		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
		finish();
	}
}
